﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

/// <summary>
/// Summary description for Translator
/// </summary>
public class Translator
{
    private static string currentLanguage = "dutch";

    private Dictionary<string, string> english;
    private Dictionary<string, string> dutch;

    //Setting up the language, if language is not found then the default language will be dutch
    public void setLanguage(String language)
    {
        switch (language)
        {
            case "dutch":
                currentLanguage = "dutch";
                break;
            case "english":
                currentLanguage = "english";
                break;
            default:
                currentLanguage = "dutch";
                break;
        }
    }

    //The hidden constructor
    public Translator()
    {
        using (debioscoopEntities db = new debioscoopEntities())
        {
            // List<translation> taal = db.translations.ToList();
            List<translation> vertalingen = db.translations.ToList();

            dutch = new Dictionary<string, string>();
            english = new Dictionary<string, string>();

            vertalingen.ForEach(vertaling =>{
                if (vertaling.taalid == 1)
                {
                    addTranslation(vertaling, dutch, (int)vertaling.taalid);
                }
                else
                {
                    addTranslation(vertaling, english, (int)vertaling.taalid);
                }
            });
        }
    }

    private void addTranslation(translation taal, Dictionary<string, string> language, int taalID)
    {
        if (taal.taalid.Value.Equals(taalID))
        {
            if (language.Keys.Equals(taal) != true)
            {
                language.Add(taal.original, taal.translation_text);
            }
        }
    }

    //Returns the correct translation
    public string getTranslation(string translateID)
    {
        if (translateID != null)
        {
            switch (currentLanguage)
            {
                case "dutch":
                    if (dutch.ContainsKey(translateID))
                    {
                        return dutch[translateID];
                    }
                    break;
                case "english":
                    if (english.ContainsKey(translateID))
                    {
                        return english[translateID];
                    }
                    break;
                default:
                    return "";
            }
        }

        return "";
    }

    public string getCurrentLanguage()
    {
        return currentLanguage;
    }

    //Get all controls and translate them to the current language
    public void pageTranslator(ControlCollection tempControls)
    {
        foreach (Control tempControl in tempControls)
        {
            if (tempControl is Label)
            {
                Label tempLabel = tempControl as Label;
                if (tempLabel != null)
                {
                    String translation = getTranslation(tempLabel.ID);
                    if (!translation.Equals(""))
                    {
                        tempLabel.Text = translation;
                    }
                }
            }

            if (tempControl is HyperLink)
            {
                HyperLink tempHyperLink = tempControl as HyperLink;
                if (tempHyperLink != null)
                {
                    String translation = getTranslation(tempHyperLink.ID);
                    if (!translation.Equals(""))
                    {
                        tempHyperLink.Text = translation;
                    }
                }
            }

            if (tempControl is Button)
            {
                Button tempButton = tempControl as Button;
                if (tempButton != null)
                {
                    String translation = getTranslation(tempButton.ID);
                    if (!translation.Equals(""))
                    {
                        tempButton.Text = translation;
                    }
                }
            }

            if (tempControl.Controls.Count > 0)
                pageTranslator(tempControl.Controls);
        }
    }
}